home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / demos / puzzle / picture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.6 KB  |  170 lines

  1. /* $XConsortium: picture.c,v 1.7 91/08/21 11:48:53 rws Exp $ */
  2. /* Puzzle - (C) Copyright 1987, 1988 Don Bennett.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <X11/Xos.h>
  13. #include <X11/X.h>
  14. #include <X11/Xlib.h>
  15. #include <errno.h>
  16.  
  17. extern Display    *dpy;
  18. extern int    screen;
  19. #ifndef MSDOS
  20. extern int    errno;
  21. #endif
  22. extern int    CreateNewColormap;
  23. extern Colormap PuzzleColormap;
  24.  
  25. /*
  26.  * This function reads a file that contains a color-mapped pixmap, in the
  27.  * following format:
  28.  * (a) width of the pixmap, first 4 bytes;
  29.  * (b) height of the pixmap next 4 bytes;
  30.  * (c) number of colormap entries, 1 byte;
  31.  * (d) colormap values, each is one byte each of (r,g,b),
  32.  *        3xnum_colormap_entries;
  33.  * (e) pixmap data, one byte per pixel;
  34.  * 
  35.  *******************************************************
  36.  * Thanks to Jack Palevich @apple, whose code I used
  37.  * for generating the color-mapped version of the mandrill
  38.  * from an original image of 8-bits per component.
  39.  *******************************************************
  40.  */
  41.  
  42. Pixmap PictureSetup(fname,width,height)
  43. char *fname;
  44. long *width, *height; /* RETURNS */
  45. {
  46. int readcount;
  47.  
  48.     int fd, i, cmapSize;
  49.     unsigned char cmapSizeByte;
  50.     unsigned char colormapData[256][3];
  51.     XColor colorMap[256];
  52.     Pixmap PicturePixmap;
  53.     unsigned char *data;
  54.     unsigned long swaptest = 1;
  55.     
  56.     fd = open(fname, O_RDONLY);
  57.     if (fd == -1) {
  58.     fprintf(stderr,"could not open picture file '%s', errno = %d\n",
  59.         fname, errno);
  60.     exit(1);
  61.     }
  62.  
  63.     read(fd, (char *)width, sizeof(*width));
  64.     read(fd, (char *)height, sizeof(*height));
  65.     if (*(char *) &swaptest) {
  66.     swap_long((char *) width, sizeof(*width));
  67.     swap_long((char *) height, sizeof(*height));
  68.     }
  69.  
  70.     read(fd, (char *)&cmapSizeByte, sizeof(cmapSizeByte));
  71.  
  72.     cmapSize = cmapSizeByte;
  73.     if (cmapSize == 0)
  74.     cmapSize = 256;
  75.  
  76.     read(fd, (char *)colormapData, 3*cmapSize);
  77.     data = (unsigned char *) malloc((*width)*(*height));
  78.     if (!data) {
  79.     fprintf(stderr,
  80.         "could not allocate memory for picture '%s', errno = %d\n",
  81.         fname, errno);
  82.     exit(1);
  83.     }
  84.     readcount = read(fd, (char *)data, (*width)*(*height));
  85.  
  86.     /***********************************/
  87.     /** allocate the colormap entries **/
  88.     /***********************************/
  89.  
  90.     for (i=0; i<cmapSize; i++) {
  91.     colorMap[i].red   = colormapData[i][0];
  92.     colorMap[i].green = colormapData[i][1];
  93.     colorMap[i].blue  = colormapData[i][2];
  94.     colorMap[i].red   |= colorMap[i].red   << 8;
  95.     colorMap[i].green |= colorMap[i].green << 8;
  96.     colorMap[i].blue  |= colorMap[i].blue  << 8;
  97.     }
  98.  
  99.     getColormapEntries(colorMap,cmapSize);
  100.  
  101.     /******************************************************/
  102.     /** convert the data to the appropriate pixel value, **/
  103.     /** cache the data as a pixmap on the server         **/
  104.     /******************************************************/
  105.       
  106.     for (i=0; i<(*width)*(*height); i++)
  107.     data[i] = colorMap[data[i]].pixel;
  108.  
  109.     {
  110.     GC gc;
  111.     XImage *image;
  112.  
  113.     gc = DefaultGC(dpy, screen);
  114.     image = XCreateImage(dpy, DefaultVisual(dpy,screen),
  115.                  8, ZPixmap, 0, (char *)data, *width, *height,
  116.                  8, *width);
  117.  
  118.     PicturePixmap = XCreatePixmap(dpy,RootWindow(dpy,screen),
  119.                       image->width,image->height,8);
  120.     XPutImage(dpy,PicturePixmap,gc,image,0,0,0,0,
  121.           image->width,image->height);
  122.     }
  123.     return(PicturePixmap);
  124. }
  125.  
  126. getColormapEntries(colorMap, numColors)
  127. XColor *colorMap;
  128. int numColors;
  129. {
  130.     int i;
  131.  
  132.     if (CreateNewColormap)
  133.     PuzzleColormap = XCreateColormap(dpy,RootWindow(dpy,screen),
  134.                      DefaultVisual(dpy,screen),AllocNone);
  135.     else
  136.     PuzzleColormap = DefaultColormap(dpy, screen);
  137.     
  138.     for (i = 0; i < numColors; i++) {
  139.     /* Need a read-only color for this value */
  140.     if (!XAllocColor(dpy,PuzzleColormap,&colorMap[i])) {
  141.         fprintf(stderr, "not enough colors (asked for %d, got %d).\n",
  142.             numColors, i);
  143.         fprintf(stderr, "try the -cm option.\n");
  144.         return (-1);
  145.     }
  146.     }
  147.     return(0);
  148. }
  149.  
  150. swap_long (bp, n)
  151.     register char *bp;
  152.     register unsigned n;
  153. {
  154.     register char c;
  155.     register char *ep = bp + n;
  156.     register char *sp;
  157.  
  158.     while (bp < ep) {
  159.     sp = bp + 3;
  160.     c = *sp;
  161.     *sp = *bp;
  162.     *bp++ = c;
  163.     sp = bp + 1;
  164.     c = *sp;
  165.     *sp = *bp;
  166.     *bp++ = c;
  167.     bp += 2;
  168.     }
  169. }
  170.